package com.liqi.web.service.device;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liqi.common.core.domain.PageData;
import com.liqi.common.core.domain.PageParam;
import com.liqi.common.core.domain.R;
import com.liqi.dev.domain.DvPlugin;
import com.liqi.dev.domain.DvPluginVersion;
import com.liqi.dev.mapper.DvPluginVersionMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author tianye
 * @date 2023-04-03
 */
@Service
public class DvPluginVersionService extends ServiceImpl<DvPluginVersionMapper, DvPluginVersion> implements IService<DvPluginVersion> {

    @Autowired
    private DvPluginVersionMapper dvPluginVersionMapper;

    // 分页
    public R<PageData<DvPluginVersion>> page(PageParam<DvPluginVersion> pageParam) {
        DvPluginVersion dvPluginVersion = pageParam.getData();
        QueryWrapper<DvPluginVersion> qw = new QueryWrapper<>();
        if (dvPluginVersion != null) {
            // 条件
        }
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize(), pageParam.getOrderBy());
        List<DvPluginVersion> list = dvPluginVersionMapper.selectList(qw);
        return R.ok(PageData.page(PageInfo.of(list)));
    }

    // 版本升级
    @Transactional
    public void version(DvPlugin dbPlugin) {
        DvPluginVersion version = new DvPluginVersion();
        BeanUtils.copyProperties(dbPlugin, version);
        version.setVersionId(null);
        dvPluginVersionMapper.insert(version);
    }

    // 版本列表
    public List<DvPluginVersion> versionList(String pluginCode) {
        QueryWrapper<DvPluginVersion> qw = new QueryWrapper<>();
        qw.eq("plugin_code", pluginCode);
        qw.orderByDesc("version_id");
        return dvPluginVersionMapper.selectList(qw);
    }

    // 删除历史版本
    public void delete(Integer pluginId) {
        QueryWrapper<DvPluginVersion> qw = new QueryWrapper<>();
        qw.eq("plugin_id", pluginId);
        dvPluginVersionMapper.delete(qw);
    }

}
